package com.tyd.cams.ui.MyCenter;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import com.tyd.cams.CamsApplication;
import com.tyd.cams.KaoqinjiluActivity;
import com.tyd.cams.LoginActivity;
import com.tyd.cams.R;
import com.tyd.cams.ZycjcxActivity;
import com.tyd.cams.utils.SPUtils;
import com.xuexiang.xui.widget.textview.supertextview.SuperTextView;

import static com.tyd.cams.CamsApplication.context;

public class MyCenterFragment extends Fragment {
    SuperTextView menu_logout,
            menu_change_account,
            kqjl;

    TextView us_leixin,
            us_zh,
            us_na;

    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        SharedPreferences preferences = context.getSharedPreferences("SP", Context.MODE_PRIVATE);
        View root = inflater.inflate(R.layout.fragment_notifications, container, false);
        menu_logout = root.findViewById(R.id.menu_logout);
        menu_change_account = root.findViewById(R.id.menu_change_account);
        kqjl = root.findViewById(R.id.kqjl);

        us_leixin = root.findViewById(R.id.us_leixin);
        us_zh = root.findViewById(R.id.us_zh);
        us_na = root.findViewById(R.id.us_na);
        if (SPUtils.getUsertype().equals("1")){
            us_leixin.setText("老师");
        }else{
            us_leixin.setText("学生");
            kqjl.setVisibility(View.GONE);
        }
        kqjl.setOnClickListener(v -> {
            Intent intent = new Intent(getActivity(), KaoqinjiluActivity.class);
            startActivity(intent);
        });

        us_zh.setText(SPUtils.getaccount());
        us_na.setText(SPUtils.getUsername());
        menu_change_account.setOnClickListener(v -> {
            /*跳转登陆页面LoginActivity*/
            Intent intent = new Intent();
            intent.setAction("android.intent.action.cams.LOGIN");
            startActivity(intent);
        });
        menu_logout.setOnClickListener(v -> {
            SharedPreferences.Editor editor = preferences.edit();
            editor.remove("token");
            editor.remove("userInfo");
            editor.remove("username");
            editor.remove("account");
            editor.remove("usertype");
//            editor.remove("isLogin");
            editor.putBoolean("isLogin", true);
            editor.commit();
            Toast.makeText(getContext(), "正在退出" + preferences.getBoolean("isLogin", true), Toast.LENGTH_SHORT).show();

            getActivity().finish();
        });
        return root;
    }
}